//
//  RGMainPageShareItem.m
//  RedGrass
//
//  Created by wukong on 2018/12/14.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//  50,80

#import "RGMainPageShareItem.h"

@interface RGMainPageShareItem()

@property (strong, nonatomic) UIImageView *icomImg;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIButton *actionBtn;


@end

@implementation RGMainPageShareItem


+(RGMainPageShareItem *)initShareItemWithIconImg:(NSString *)imgUrl title:(NSString *)title{
    RGMainPageShareItem *item = [[RGMainPageShareItem alloc]init];
    if([imgUrl rangeOfString:@"http"].length != 0){
        
    }else{
        item.icomImg.image = IMAGE_NAMED(SafeStr(imgUrl));
    }
    if(title.length == 0){

        [item.titleLabel removeFromSuperview];
        [item.icomImg mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(item).mas_offset(15);
           
        }];
    }else{
         item.titleLabel.text = SafeStr(title);
    }
    
    
    return item;
}

-(instancetype) initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
//        self.backgroundColor = [UIColor grayColor];
        [self initUI];
        [self addMasonry];
    }
    return self;
}

-(void)initUI{
    
    [self addSubview:self.icomImg];
    [self addSubview:self.titleLabel];
    [self addSubview:self.actionBtn];
}

-(void) addMasonry{
   
    [_icomImg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).mas_offset(4);
        make.centerX.mas_equalTo(self).mas_offset(0);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).mas_offset(6);
        make.right.mas_equalTo(self).mas_offset(-6);
        make.top.mas_equalTo(self.icomImg.mas_bottom).mas_offset(8);
    }];
    
    [_actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}


#pragma mark - reponse

-(void)actionBtnClick:(UIButton *)button{
    if(self.actionBtnClickBlock){
        self.actionBtnClickBlock(self.tag);
    }
}


#pragma mark - getter

-(UIImageView *)icomImg{
    if(!_icomImg){
        _icomImg = [[UIImageView alloc]init];
    }
    return _icomImg;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = FONT(customFontName, FFont_12);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _titleLabel;
}

-(UIButton *) actionBtn{
    if(!_actionBtn){
        _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_actionBtn addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _actionBtn;
}


@end
